#include <bits/stdc++.h>
#define re register
#define int long long
#define Add(a,b) (((a) + (b)) % mod)
#define Mul(a,b) ((a) * (b) % mod)
#define Div(a,b) (Mul(a,qmi(b,mod - 2)))
#define chAdd(a,b) (a = Add(a,b))
#define chMul(a,b) (a = Mul(a,b))

using namespace std;

const int N = 5010;
const int mod = 1e9 + 7;
int n,k;
int arr[N],val[N];
int fac[N],infac[N];
vector<int> g[N];

inline int read(){
    int r = 0,w = 1;
    char c = getchar();
    while (c < '0' || c > '9'){
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9'){
        r = (r << 3) + (r << 1) + (c ^ 48);
        c = getchar();
    }
    return r * w;
}

inline int qmi(int a,int b){
    int res = 1;
    while (b){
        if (b & 1) chMul(res,a);
        chMul(a,a); b >>= 1;
    } return res;
}

inline void init(int n){
    fac[0] = 1;
    for (re int i = 1;i <= n;i++) fac[i] = Mul(fac[i - 1],i);
    infac[n] = qmi(fac[n],mod - 2);
    for (re int i = n - 1;~i;i--) infac[i] = Mul(infac[i + 1],i + 1);
}

namespace Sub1{
    const int N = 24,M = (1 << 10) + 10;
    int fp[N],dep[N];
    int dp[N][M],f[N][M];

    inline void dfs(int u,int fa){
        dep[u] = dep[fp[u] = fa] + 1;
        for (int v:g[u]){
            if (v == fa) continue;
            dfs(v,u);
        }
    }

    inline int get(int x,int y){
        int res = val[x] | val[y];
        if (dep[x] < dep[y]) swap(x,y);
        while (dep[x] != dep[y]) res |= val[x = fp[x]];
        while (x != y) res |= (val[x = fp[x]] | val[y = fp[y]]);
        return res;
    }

    inline void solve(){
        int ans = 0; init(n);
        dfs(1,0); f[0][0] = 1;
        for (re int i = 0;i < n;i++){
            for (re int st = 0;st < (1 << n);st++){
                int tmp = ((1 << n) - 1) ^ st;
                for (re int S = tmp;S;S = (S - 1) & tmp){
                    vector<int> v;
                    for (re int i = 1;i <= n;i++){
                        if ((S >> (i - 1)) & 1) v.push_back(i);
                    } int res = 0;
                    for (int x:v){
                        for (int y:v) res |= get(x,y);
                    } chAdd(dp[i + 1][st | S],Add(dp[i][st],Mul(f[i][st],arr[__builtin_popcount(res)])));
                    chAdd(f[i + 1][st | S],f[i][st]);
                    // cerr << S << " " << res << " ???\n";
                }
            }
        }
        for (re int i = 1;i <= n;i++) chAdd(ans,Mul(dp[i][(1 << n) - 1],infac[i]));
        printf("%lld\n",ans);
    }
}

signed main(){
    freopen("water.in","r",stdin);
    freopen("water.out","w",stdout);
    n = read(),k = read();
    for (re int i = 1;i <= k;i++) arr[i] = read();
    for (re int i = 1,len;i <= n;i++){
        len = read();
        while (len--) val[i] |= (1 << (read() - 1));
    }
    for (re int i = 1,a,b;i < n;i++){
        a = read(),b = read();
        g[a].push_back(b);
        g[b].push_back(a);
    }
    if (n <= 10) Sub1::solve();
    return 0;
}